<?php

if (isset($_POST["PHPSESSID"])) {
    session_id($_POST["PHPSESSID"]);
}
session_start();

try {
	//创建mongo数据库连接对象ß
	$m  = new Mongo();
	//选择数据库
	$db = $m->gshow;
	//选择表
	$grid = $db->getGridFS();
	
	$name = $_FILES['upfile']['name'];
	$type = $_FILES['upfile']['type'];        // Try to get file extension
    
	print_r($_FILES['upfile']);
	//die;
	
	//保存文件
	$id = $grid->storeUpload('upfile', $name);
	$type= "";

	$ext = end(explode('.', $name));
	switch ($ext) {
	
      case "jpg": $type = "image/jpeg"; break;
      case "gif": $type = "image/gif"; 	break;
      case "png": $type = "image/png"; 	break;
      case "txt": $type = "text/plain"; break;
      case "pdf": $type = "application/pdf"; break;
      case "zip": $type = "application/x-zip"; break;
	
	}
	
	//更新文件信息
	$files = $db->fs->files;
	//var_dump($files);
	//die;
	$files->update(array("filename" => $name), array('$set' => array("contentType" => $type, "aliases" => null, "metadata" => null)));
	//显示列表
	
	$cursor = $grid->find();
	foreach ($cursor as $obj) { ?>

      <img src="gridfs_getfile.php?filename=<?php echo $obj->getFilename(); ?>" width="150" />
	
	<?php
	}
	$conn->close();
	echo "<a href='gridfs_upload_image.html' target='_self'>返回继续上传</a>";
	
} catch(Exception $e) {
       echo $e->message();
}
	
	 